/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradeplus.admin.dao;

import com.tradeplus.admin.model.SystemProperty;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author ronnie
 */
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Repository
public class SytemPropertyDaoImpl implements SystemPropertyDao {
    
    private HibernateTemplate hibernateTemplate;
    
    /**
     * Sets HibernateTemplate instance to hibernateTemplate object (through dependency injection)
     *
     * @param jpaTemplate
     */
    public void setJpaTemplate(HibernateTemplate jpaTemplate) {
            this.hibernateTemplate = jpaTemplate;
    }

    /**
     * Performs hibernate save operation for SystemProperty
     * 
     * @param region
     * @return populated SystemProperty object
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public SystemProperty save(SystemProperty systemProperty) {
        systemProperty = (SystemProperty)hibernateTemplate.merge(systemProperty);
        
        return systemProperty;
    }

    /**
     * Performs hibernate delete operation for SystemProperty
     * 
     * @param systemProperty
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(SystemProperty systemProperty) {
        systemProperty = (SystemProperty)hibernateTemplate.merge(systemProperty);	
        hibernateTemplate.delete(systemProperty);
    }

    /**
     * Retrieves a particular SystemProperty object for given systemPropertyID
     * 
     * @param systemPropertyID
     * @return SystemProperty object
     */
    @Override
    public SystemProperty get(int systemPropertyId) {
        return (SystemProperty)hibernateTemplate.get(SystemProperty.class,systemPropertyId);
    }

    /**
     * Finds a particular SystemProperty object for corresponding key
     * 
     * @param key
     * @return SystemProperty object
     */
    @Override
    public SystemProperty findByKey(String key) {
        SystemProperty systemProperty = null;
        List<SystemProperty> systemProperties = (List<SystemProperty>)hibernateTemplate.find("from SystemProperty where key=?",key);

        if(systemProperties != null && systemProperties.size() > 0) {
            if(systemProperties.size() == 1) {
                systemProperty = systemProperties.get(0);
            } else {
                throw new javax.persistence.NonUniqueResultException("Found more than one SystemProperty for key:"+key);
            }
        }
        
        return systemProperty;
    }

    /**
     * Retrieves all SystemPropertys
     * 
     * @return list of SystemProperty objects
     */
    @Override
    public List<SystemProperty> getAllSystemProperties() {
        return hibernateTemplate.find("from SystemProperty");
    }
    
}
